//
//  ABTongxunlu.m
//  Fengshui
//
//  Created by are on 13-5-28.
//
//

#import "ABTongxunlu.h"

@implementation ABTongxunlu
- (id)init {

    self = [super init];

    if (self) {

        self.addressbookArray = [NSMutableArray array];

    }

    return self;
}
-(void)ReadAllPeoples

{

    //取得本地通信录名柄

 

    ABAddressBookRef tmpAddressBook = ABAddressBookCreate();

    //取得本地所有联系人记录

    NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
    
    for(id tmpPerson in tmpPeoples)

    {

        //获取的联系人单一属性:First name

        NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);

        [tmpFirstName release];
         ABRecordID recordID=ABRecordGetRecordID(tmpPerson);
        Dlog(@"%d",recordID);
        //获取的联系人单一属性:Last name

        NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
        
        [tmpLastName release];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        if (tmpLastName||tmpFirstName) {
            
            NSString *nameString = [NSString string];
            if (tmpLastName) {
                
                nameString = [nameString stringByAppendingString:tmpLastName];
            }
            if (tmpFirstName) {

                nameString  = [nameString stringByAppendingString:tmpFirstName];
                
            }
 
            [dic setObject:nameString forKey:@"name"];
            

}
        else
        {
            continue;
        }
        //获取的联系人单一属性:Nickname

//        NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty);
//
//        Dlog(@"Nickname:%@", tmpNickname);
//
//        [tmpNickname release];
//
        //获取的联系人单一属性:Company name

//        NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);
//
//        Dlog(@"Company name:%@", tmpCompanyname);
//        
//        [tmpCompanyname release];
//
        //获取的联系人单一属性:Job Title
        
//        NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);
//
//        Dlog(@"Job Title:%@", tmpJobTitle);
//        
//        [tmpJobTitle release];
//
//        //获取的联系人单一属性:Department name
//
//        NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);
//
//        Dlog(@"Department name:%@", tmpDepartmentName);
//        
//        [tmpDepartmentName release];
            
//        //获取的联系人单一属性:Email(s)
//
//        ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);
//
//        for(NSInteger j = 0; ABMultiValueGetCount(tmpEmails); j++)
//
//        {
//
//            NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
//
//            Dlog(@"Emails%d:%@", j, tmpEmailIndex);
//
//            [tmpEmailIndex release];
//
//        }
//
//        if (tmpEmails) {
//
//             CFRelease(tmpEmails);
//        }


        //获取的联系人单一属性:Birthday

        NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty);
        
   
        
        [tmpBirthday release];
        
        //获取的联系人单一属性:Note
        
        NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty);
        
      
        
        [tmpNote release];
        
        //获取的联系人单一属性:Generic phone number
        
        ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty);
        if (ABMultiValueGetCount(tmpPhones)<1) {

            continue;
            
        }
        NSMutableArray *array = [NSMutableArray array];
        for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++)
        {
            NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones,j);
          
            [array addObject:tmpPhoneIndex];
            [tmpPhoneIndex release];
            
        }
        [dic setObject:array forKey:@"cellPhone"];
          
        if (tmpPhones) {

             CFRelease(tmpPhones);
        }
        NSData *imageData = (NSData *)ABPersonCopyImageData(tmpPerson);
        if (imageData) {

            [dic setObject:imageData forKey:@"image"];
            
        }

    [self.addressbookArray addObject:dic];
       
    }
        
    //释放内存 
  
    [tmpPeoples release];
    
    CFRelease(tmpAddressBook);}
@end
